package net.kts.story;


import java.util.concurrent.TimeUnit;

import net.kts.help.HelpLayout;
import net.kts.help.HelpPreferences;
import net.kts.help.HelpService;
import net.kts.main.R;
import net.kts.story.progress.StoryProgressDialog;
import net.kts.story.twitter.contator.TwitterConator;
import net.kts.story.twitter.oauth.OAuth;
import net.kts.transrate.TranslateService;
import twitter4j.TwitterException;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;

public class Story extends MapActivity{
	private MapView mapView;
	private TwitterConator twiitterConator;
	private StoryObjectHouse storyObjectHouse;
	private Story story;
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.story_main);
		StoryProgressDialog.showProgressDiaglog();
		this.story = this;
		
		Thread thread = new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					createMapView();
					createTwitterConator();
					StoryProgressDialog.doItExecute(new Runnable() {
						@Override
						public void run() {
							storyObjectHouse = new StoryObjectHouse(story, mapView, twiitterConator);
							storyObjectHouse.updateWidthNewLocation();
							StoryProgressDialog.dismissProgressDiaglog();
						}
					});
				} catch (Exception e) {
					StoryProgressDialog.doItExecute(new Runnable() {
						@Override
						public void run() {
							Toast.makeText(story, 
									TranslateService.translateRepository("인터넷이 불안정 합니다."), 1).show();
							StoryProgressDialog.dismissProgressDiaglog();
						}
					});
				}
			}
		});
		thread.start();
		
		if(HelpPreferences.isShowing())
	        new HelpService(this)
	        		.add(new HelpLayout(R.drawable.help_story_menu));
	}	
	

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode  == RESULT_OK){
			if (requestCode == 1)
			if (data.getStringExtra("PIN") != null){
				twiitterConator.obtainCertification(data.getStringExtra("PIN"));
				storyObjectHouse.updateWidthNewLocation();
				Toast.makeText(this, TranslateService.translateRepository("please the refresh button"), 1).show();
			}
					
			if (requestCode == 2) 
			if(data.getData() != null)
					storyObjectHouse.getStoryMenu().addAttachTweetImage(data.getData());
		}
	}
	
	private void createMapView() {
		this.mapView = (MapView) findViewById(R.id.hithereMap);
		this.mapView.getController().setZoom(17);
	}
	
	private void createTwitterConator() {
		try {
			OAuth oAuth = new OAuth(this);
			twiitterConator = new TwitterConator(this, oAuth);
			Log.d("twitter", "OAuthActivity created" + twiitterConator.toString());
		} catch (TwitterException e) {
			Log.e("twitter", e.getErrorMessage());
		}
	}


	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
	
	private boolean isFinising = false;
    @Override
    public void onBackPressed() {
    	if(isFinising){
    		moveTaskToBack(true);
    		android.os.Process.killProcess(android.os.Process.myPid());
    	}
    	Toast.makeText(this, TranslateService.translateRepository("다시 한번 눌러 종료하세요"), 1).show();
    	Thread timeWorker = new Thread(new Runnable() {			
			@Override
			public void run() {
				isFinising = true;
				try {
					TimeUnit.SECONDS.sleep(2);
					isFinising = false;
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		});
    	timeWorker.start();
    }
}
